﻿using AutoMapper;
using MediatR;
using Process.API.Application.Command.Action.Report;
using Process.Domain;
using Process.ErrorCode;
using Process.Infrastructure.Implement;
using Serilog;

namespace Process.API.Application.Handler.Action.Report
{
    public class AddReportHandler:IRequestHandler<AddReportCommand ,APIResult<int>>
    {
        DatabaseContext Db;
        IMapper mapper;

        public AddReportHandler(DatabaseContext db, IMapper mapper)
        {
            Db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(AddReportCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            request.IsDel = false;
            var rep = mapper.Map<ReportTable>(request);
            var m = await Db.SqlSugar.Insertable(rep).ExecuteCommandAsync();

            result.Code = APIEnum.Ok;
            result.Message = "报工记录添加成功";
            result.Data = m;
            return await Task.FromResult(result);
        }
    }
}
